package com.example.hrandroidapp.preferences;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.hrandroidapp.domain.UserPO;

public class UserPreference {
	
	private static final String PARAM_BRANCH_ID = "branchId";
	private static final String PARAM_NAME = "name";
	private static final String PARAM_BRANCH_NAME = "branchName";
	private static final String PARAM_USER_ID = "useId";
	private static final String PARAM_LOGIN_ID = "loginId";
	private static final String PARAM_PASSWORD = "password";
	
	private static final String USERPO_FILE_NAME = "userPO";
	
	
    public UserPreference() {}
	
	public UserPO getUser(Context context) {
        UserPO user = new UserPO();
        SharedPreferences prefs = context.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
        user.setUserId(prefs.getString(PARAM_USER_ID, null));
        user.setLoginId(prefs.getString(PARAM_LOGIN_ID, null));
        user.setBranchId(prefs.getLong(PARAM_BRANCH_ID, 0));
        user.setName(prefs.getString(PARAM_NAME, null));
        user.setBranchName(prefs.getString(PARAM_BRANCH_NAME, null));
        user.setBranchName(prefs.getString(PARAM_PASSWORD, null));
       
        return user;
    }
	 
    public static void updateUser(Context context, UserPO user) {
        SharedPreferences prefs = context.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(PARAM_USER_ID, user.getUserId()).commit();
        prefs.edit().putString(PARAM_LOGIN_ID, user.getLoginId()).commit();
        prefs.edit().putLong(PARAM_BRANCH_ID, user.getBranchId()).commit();
        prefs.edit().putString(PARAM_NAME, user.getUserId()).commit();
        prefs.edit().putString(PARAM_BRANCH_NAME, user.getUserId()).commit();
        prefs.edit().putString(PARAM_PASSWORD, user.getUserId()).commit();
    }
    
    public static void clearUser(Context context) {
    	SharedPreferences settings = context.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
    	settings.edit().remove(PARAM_USER_ID).commit();
    	settings.edit().remove(PARAM_LOGIN_ID).commit();
    	settings.edit().remove(PARAM_BRANCH_ID).commit();
    	settings.edit().remove(PARAM_NAME).commit();
    	settings.edit().remove(PARAM_BRANCH_NAME).commit();
    	settings.edit().remove(PARAM_PASSWORD).commit();
    }

    public static Long getBranchId(Context ctx)
    {
    	SharedPreferences prefs = ctx.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
    	return prefs.getLong(PARAM_BRANCH_ID, 0);
    	
    }
    
    public static String getUserId(Context ctx)
    {
    	SharedPreferences prefs = ctx.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
    	return prefs.getString(PARAM_USER_ID, null);
    }
    
    public static String getLoginId(Context ctx)
    {
    	SharedPreferences prefs = ctx.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
    	return prefs.getString(PARAM_LOGIN_ID, null);
    }
    
    public static String getBranchName(Context ctx) {
    	SharedPreferences prefs = ctx.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
    	return prefs.getString(PARAM_BRANCH_NAME, null);
    }
    
    public static String getName(Context ctx) {
    	SharedPreferences prefs = ctx.getSharedPreferences(USERPO_FILE_NAME, Context.MODE_PRIVATE);
    	return prefs.getString(PARAM_NAME, null);
    }
    
}
